package com.atguigu.alipay.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.atguigu.alipay.config.AliPayConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author panghl
 * @Date 2021/2/5 11:47
 * @Description TODO
 **/
@RestController
@RequestMapping("/edualipay/")
@Slf4j
public class AliPayController {

    @Autowired
    private AliPayConfig aliPayConfig;

    @PostMapping("/pay")
    public String pay(String out_trade_no, String subject, String total_amount, String body,
                      String time_out_express, String product_code) throws AlipayApiException {
        //1.封装Rsa签名方式
//        AlipayClient client = new DefaultAlipayClient(aliPayConfig.URL,
//                aliPayConfig.APPID, aliPayConfig.RSA_PRIVATE_KEY,
//                aliPayConfig.FORMAT, aliPayConfig.CHARSET,
//                aliPayConfig.ALIPAY_PUBLIC_KEY, aliPayConfig.SIGNTYPE);

        //2.创建Request请求
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();

        //封装传入参数
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();

        model.setOutTradeNo(out_trade_no); //订单号
        model.setBody(body);    //描述
        model.setProductCode(product_code); //商品码
        model.setSubject(subject);  //主题
        model.setTotalAmount(total_amount);    //价格
        model.setTimeoutExpress(time_out_express);   //超时时间

        //设置请求参数
        request.setBizModel(model);
        //设置异步回调地址
//        request.setNotifyUrl(aliPayConfig.notify_url);
        //设置同步回调地址
//        request.setReturnUrl(aliPayConfig.return_url);

        //生成表单
//        String form = client.pageExecute(request).getBody();
        //如果支付失败的补偿机制
        log.info("确认订单[{}]付款成功", out_trade_no);
        //修改订单状态 ...

//        return form;
        return null;
    }
}
